package middle;

import java.util.*;

/**
 * 3. 无重复字符的最长子串
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        String str = "helloworld";
        int size = lengthOfLongestSubstring(str);
        System.out.println(size);
    }

    public static int lengthOfLongestSubstring(String s) {
        int result = 0;
        int current = 0;
        char[] chars = s.toCharArray();
        List<Character> set = new ArrayList<>();
        for (char c:chars){
            if (set.contains(c)){
                int index = set.indexOf(c);
                if (current>result){
                    result = current;
                }
                current = current-index;
                for (int i=0;i<=index;i++){
                    set.remove(0);
                }
            }else {
                current++;
            }
            set.add(c);
        }
        if (current>result){
            result = current;
        }
        System.out.println("s = " + Arrays.toString(set.toArray()));
        return result;
    }

}
